(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.job.createContinueController', createContinueController);
	createContinueController.$inject = [ '$scope', 'Constants', '$q', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'jobService' ];
	function createContinueController($scope, Constants, $q, modalService, gridService, layoutConfig, validatorConfig, authService, jobService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.job = {};
		ctrl.job.jobDepencyList = ctrl.job.jobDepencyList || [];
		
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobDepency.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.job.jobDepencyList';
			//add sub data validator
			if(ctrl.validatorConfig) ctrl.layoutConfig.formConfigs.jobParameters.tableConfigs.table.validatorConfig = ctrl.validatorConfig.jobParmetes;
			ctrl.jobParmetesOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobParameters.tableConfigs.table,parSelectionChanged);
			ctrl.jobParmetesOptions.data = 'vm.job.jobParmetes';
			jobService.initDataOptions(ctrl);
			authService.createContinue(params).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.job = response.data.model;
					ctrl.job.taskId = response.data.task.taskId;
					ctrl.job.jobDepencyList = ctrl.job.jobDepencyList || [];
				}
			});
		};
		ctrl.addJobDepency = function(row) {
			
			var deferred = $q.defer();
			
			var promise = deferred.promise;

			if (!ctrl.job.jobGroupName) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.job.parameter.name.can.not.repeate' ]
				});
				deferred.reject();
			}else{
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.job',
					'title' : 'title.dialogs.job',
					'params' : {
						"jobDepencyList" : ctrl.job.jobDepencyList,
						"jobGroupId" : ctrl.job.jobGroupId
					},
					'btnCallback' : {
						'ok' : function(selectedRows) {
							if (!selectedRows || selectedRows.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								deferred.reject();
							}
							angular.merge(row, selectedRows[0]);
							row.dataStatus = "";
							deferred.resolve();
							return promise;
						},
						'close' : function(){
							deferred.reject();
						}
					}
				});
			}
			return promise;
		};
		
		ctrl.jobGroupChange = function(value){
			ctrl.job.jobDepencyList = [];
		};
		
		ctrl.createContinueSave = function() {
			if(jobService.checkParameter(ctrl.job.jobParmetes)){
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.job.parameter.name.can.not.repeate' ]
				});
				return;
			}
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else {
				return authService.createContinueSave(ctrl.job).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createContinueSubmit = function() {
			if(jobService.checkParameter(ctrl.job.jobParmetes)){
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.job.parameter.name.can.not.repeate' ]
				});
				return;
			}
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else {
				return authService.createContinueSubmit(ctrl.job).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.createContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.job.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createContinueCancel(ctrl.job).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		ctrl.addJobDepency = function(row) {
			
			var deferred = $q.defer();
			
			var promise = deferred.promise;

			if (!ctrl.job.jobGroupName) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.job.group.name.required' ]
				});
				deferred.reject();
			}else{
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.job',
					'title' : 'title.dialogs.job',
					'params' : {
						"jobDepencyList" : ctrl.job.jobDepencyList,
						"jobGroupId" : ctrl.job.jobGroupId
					},
					'btnCallback' : {
						'ok' : function(selectedRows) {
							if (!selectedRows || selectedRows.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								deferred.reject();
							}
							angular.merge(row, selectedRows[0]);
							deferred.resolve(selectedRows[0]);
						},
						'close' : function(){
							deferred.reject();
						}
					}
				});
			}
			return promise;
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedJobDepencyList = selectedRows;
		}
		function parSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedjobParameters = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.job.list',
					'state' : 'cda.job.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.job = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.job.list',
					'state' : 'cda.job.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);